package org.mineground.player.groupchat;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.mineground.Main;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @file GroupChatHandler.java (20.02.2012)
 * @author Daniel Koenen
 *
 */
public class GroupChatHandler {
    private static final Logger ExceptionLogger = LoggerFactory.getLogger(GroupChatHandler.class);
    private HashMap<Integer, GroupChat> groupChats = new HashMap<Integer, GroupChat>();
    
    public GroupChatHandler() {
       loadGroupChats();
    }
    
    private void loadGroupChats() {
        try {
            PreparedStatement preparedStatement = Main.getInstance().getDatabaseHandler().getConnection().prepareStatement("SELECT group_id, group_name, password FROM lvm_group_chats");
            preparedStatement.execute();
            
            long groupPassword;
            int groupId;
            
            String groupName;
            ResultSet queryResult = preparedStatement.getResultSet();
            GroupChat groupChat;
            
            while (queryResult.next()) {
                groupId = queryResult.getInt(1);
                groupName = queryResult.getString(2);
                groupPassword = queryResult.getLong(3);
                
                groupChat = new GroupChat(groupId, groupName, groupPassword);
                groupChats.put(groupId, groupChat);
            }
        }
        
        catch (Exception exception) {
            ExceptionLogger.error("Exception caught", exception);
        }
    }
    
    public int createGroupChat(String groupName, long password) {
        try {
            PreparedStatement preparedStatement = Main.getInstance().getDatabaseHandler().getConnection().prepareStatement("INSERT INTO lvm_group_chats (group_name, password) VALUES (?, ?)");
            preparedStatement.setString(1, groupName);
            preparedStatement.setLong(2, password);
            preparedStatement.execute();
            
            preparedStatement = Main.getInstance().getDatabaseHandler().getConnection().prepareStatement("SELECT group_id FROM lvm_group_chats WHERE group_name = ?");
            preparedStatement.setString(1, groupName);
            preparedStatement.execute();
            
            ResultSet queryResult = preparedStatement.getResultSet();
            queryResult.next();
            
            int groupId = queryResult.getInt(1);
            
            GroupChat groupChat = new GroupChat(groupId, groupName, password);
            groupChats.put(groupId, groupChat);
            return groupId;
        }
        
        catch (Exception exception) {
            ExceptionLogger.error("Exception caught", exception);
        }
        
        return -1;
    }
    
    public void removeGroupChat(int groupId) {
        try {
            PreparedStatement preparedStatement = Main.getInstance().getDatabaseHandler().getConnection().prepareStatement("DELETE FROM lvm_group_chats WHERE group_id = ?");
            preparedStatement.setInt(1, groupId);
            preparedStatement.execute();
        }
        
        catch (Exception exception) {
            ExceptionLogger.error("Exception caught", exception);
        }
    }
    
    public GroupChat getGroupChat(int groupChatId) {
        return groupChats.get(groupChatId);
    }
    
    public boolean doesGroupExist(int groupChatId) {
        return groupChats.containsKey(groupChatId);
    }
    
    public ArrayList<GroupChat> getGroupChatList(int pageNumber) {
        int iterator = 0;
	ArrayList<GroupChat> fullList = new ArrayList<GroupChat>();
	ArrayList<GroupChat> viewableList = new ArrayList<GroupChat>();

	for (Map.Entry<Integer, GroupChat> entry : groupChats.entrySet()) {
	    if (entry.getValue().getGroupPassword() == 0) {
		fullList.add(entry.getValue());
	    }
	}

	for (GroupChat groupChat : fullList) {
	    if (iterator >= (pageNumber - 1) * 5 && iterator < (pageNumber * 5)) {
		viewableList.add(groupChat);
	    }

	    iterator++;
	}

	return viewableList;
    }
    
    public int getTotalPages() {
        int index = groupChats.size(); 
        int rest = index % 5;
        int totalPages = (index - rest) / 5;
        return (rest == 0) ? (totalPages) : (totalPages + 1);
    }
}
